﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MikadoShooter
{
    public static class DllChecker
    {
        private const string Dllpath = "dll"; //dll pathdirectory

        [DllImport("kernel32", SetLastError = true)]
        private static extern IntPtr LoadLibrary(string lpFileName);

        public static bool IsMissingDlls(string[] fileList)
        {
            bool error = false;

            foreach (KeyValuePair<string, string> missing in GetRequirements(fileList))
            {
                error = true;
                MessageBox.Show(missing.Value, missing.Key);
            }

            return error;
        }

        private static Dictionary<string, string> GetRequirements(IEnumerable<string> fileList)
        {
            return fileList.Where(file => LoadLibrary(file) == IntPtr.Zero && LoadLibrary(Path.Combine(Dllpath, file)) == IntPtr.Zero)
                .ToDictionary(file => file.ToLower(), file => new Win32Exception().Message);
        }
    }
}